#!/bin/bash

while getopts ":s:" opt; do
    case $opt in
        s)
            echo "-s was triggered, Parameter: $OPTARG" >&2
            application=$OPTARG;
            application_pid_file="gunicorn_pid_$OPTARG";

            if [ $application == "app1" ] || [ $application == "login" ]; then
                application_port=5000
                echo "starting $application application on port $application_port" 
                ../bin/gunicorn -b 0.0.0.0:$application_port -w 4 $OPTARG:app &
                echo $! > $application_pid_file
                echo "pid of master worker is $!"
            else
                echo "this is not a recognized application"
                exit 1
            fi
            ;;
        \?)
            echo "Invalid option: $OPTARG" >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires and argument." >&2
            exit 1
            ;;
    esac
done

